package com.heyqing.medium.day7;

/**
 * ClassName:IslandPerimeter
 * Package:com.heyqing.medium.day7
 * Description:
 *
 * @Date:2024/10/9
 * @Author:Heyqing
 */
public class IslandPerimeter {
    public int islandPerimeter(int[][] grid) {
        int divide = 0, island = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == 1) island++;
                System.out.println((i + 1) + "行 " + (j + 1) + " 个" + "当前island " + island);
                // 上
                if (i - 1 >= 0 && j < grid[i - 1].length && grid[i][j] == 1) {
                    System.out.println("上边有");
                    if (grid[i - 1][j] == 1) divide++;
                }
                // 左
                if (j - 1 >= 0 && grid[i][j] == 1) {
                    System.out.println("左边有");
                    if (grid[i][j - 1] == 1) divide++;
                }

            }
        }
        return island * 4 - divide * 2;
    }
}
